﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Reporting.WebForms;
using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using System.IO;

public partial class Reports_TruckStatusReport : BasePage
{
    DataBase db = new DataBase();
    ISyClientHandler clientManage = new SyClientHandler();
    private static string clientName = string.Empty;
    ISystemHandler systemManage = new SystemHandler();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDDl();
            PreviewReport();
        }
    }

    #region FillDDl
    private void FillDDl()
    {
        //绑定托运人
        DataTable dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.TYR.ToString("d")));
        lstShipper.DataSource = dt;

        if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
        {
            lstShipper.DataTextField = "ENGShortName";
        }
        else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
        {
            lstShipper.DataTextField = "HKShortName";
        }
        else
        {
            lstShipper.DataTextField = "CNShortName";
        }
        lstShipper.DataValueField = "PKID";
        lstShipper.DataBind();

        //工厂
        dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.GC.ToString("d")));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem item = new ListItem();
            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                // ddlVendor.DataTextField = "HKShortName";
                item.Text = dt.Rows[i]["ENGShortName"].ToString() + "/" + dt.Rows[i]["HKShortName"].ToString();
            }
            else
            {
                item.Text = dt.Rows[i]["ENGShortName"].ToString() + "/" + dt.Rows[i]["CNShortName"].ToString();
                //ddlVendor.DataTextField = "CNShortName";
            }
            item.Value = dt.Rows[i]["PKID"].ToString();
            // ddlVendor.DataValueField = "PKID";
            lstVendor.Items.Add(item);
        }

        //仓库
        dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.CK.ToString("d")));
        lstWhse.DataSource = dt;
        if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
        {
            lstWhse.DataTextField = "ENGShortName";
        }
        else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
        {
            lstWhse.DataTextField = "HKShortName";
        }
        else
        {
            lstWhse.DataTextField = "CNShortName";
        }
        lstWhse.DataValueField = "PKID";
        lstWhse.DataBind();

        ////客户客人
        //dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.KHKR.ToString("d")));
        //lstCustomer.DataSource = dt;
        //if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
        //{
        //    lstCustomer.DataTextField = "ENGShortName";
        //}
        //else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
        //{
        //    lstCustomer.DataTextField = "HKShortName";
        //}
        //else
        //{
        //    lstCustomer.DataTextField = "CNShortName";
        //}
        //lstCustomer.DataValueField = "PKID";
        //lstCustomer.DataBind();
    }
    #endregion

    #region GetDataTable 获取主报表的数据源
    private DataTable GetDataTable()
    {
        SqlParameter[] parms = null;
        List<SqlParameter> lst = new List<SqlParameter>();
        string sqlWhere = string.Empty;
        if (ViewState["DateFrom"] != null && ViewState["DateTo"] != null)
        {
            lst.Add(new SqlParameter("@JobDateFrom", ViewState["DateFrom"].ToString() + " 00:00:00"));
            lst.Add(new SqlParameter("@JobDateTo", ViewState["DateTo"].ToString() + " 23:59:59"));
            sqlWhere += " and JobDate between @JobDateFrom and @JobDateTo ";
        }
        else if (ViewState["DateFrom"] != null && ViewState["DateTo"] == null)
        {
            lst.Add(new SqlParameter("@JobDateFrom", ViewState["DateFrom"].ToString() + " 00:00:00"));
            sqlWhere += " and JobDate >= @JobDateFrom ";
        }
        else if (ViewState["DateFrom"] == null && ViewState["DateTo"] != null)
        {
            lst.Add(new SqlParameter("@JobDateTo", ViewState["DateTo"].ToString() + " 00:00:00"));
            sqlWhere += " and JobDate <= @JobDateTo ";
        }

        if (ViewState["Shipper"] != null)
        {
            // lst.Add(new SqlParameter("@Shipper", ViewState["Shipper"].ToString()));
            sqlWhere += " and ShipperID in (" + ViewState["Shipper"].ToString() + ")";
        }

        if (ViewState["Whse"] != null)
        {
            sqlWhere += " and WhouseID in(" + ViewState["Whse"].ToString() + ")";
        }
        if (ViewState["Vendor"] != null)
        {
            // lst.Add(new SqlParameter("@Shipper", ViewState["Shipper"].ToString()));
            sqlWhere += " and VendorID in (" + ViewState["Vendor"].ToString() + ")";
        }
        if (ViewState["LinceseNo"] != null)
        {
            lst.Add(new SqlParameter("@LinceseNo", ViewState["LinceseNo"].ToString()));
            sqlWhere += " and LinceseNO =@LinceseNo ";
        }
        if (systemManage.IsCompanyRoles2())//公司用户2
        {
            lst.Add(new SqlParameter("@UserID", Session["UserID"].ToString()));

        }


        parms = lst.ToArray();
        string sqlStr =string.Empty;

        if (systemManage.IsCompanyRoles2())//公司用户2
        {
            sqlStr = @"SELECT st.PKID AS STID, stDtl.PKID AS STDtlID, soDtl.JobDate, 
shipper.ENGShortName AS Shipper, vendor.ENGShortName AS Vendor, 
 whse.ENGShortName AS Whse, soDtl.SONO, soDtl.VOL, cm.Model AS TruckType, contractor.ENGShortName AS Contractor, 
 truck.LicenseNO AS LinceseNO, Ts.StatusName, bnT.SendTruckID, bnT.CBDate
FROM         dbo.BN_SendTruck AS st INNER JOIN
                      dbo.BN_SendTruckDtl AS stDtl ON st.PKID = stDtl.SendTruckID INNER JOIN
                      dbo.BN_ShippingOrder AS so ON st.OrderID = so.PKID INNER JOIN
                      dbo.BN_ShippingOrderDtl AS soDtl ON so.PKID = soDtl.OrderID INNER JOIN
                      dbo.BN_TTracking AS bnT ON st.PKID = bnT.SendTruckID LEFT OUTER JOIN
                      dbo.Sy_TruckStatus AS Ts ON bnT.TruckStatusID = Ts.PKID INNER JOIN
                      dbo.Sy_Client AS shipper ON soDtl.ShipperID = shipper.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS vendor ON so.VendorID = vendor.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS whse ON soDtl.WhouseID = whse.PKID LEFT OUTER JOIN
                      dbo.Sy_ContainerModel AS cm ON stDtl.TypeID = cm.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS contractor ON so.Contractor = contractor.PKID LEFT OUTER JOIN
                      dbo.BN_Truck AS truck ON stDtl.TruckID = truck.PKID 
                      join bn_Booking as b 
on so.BookingID=b.PKID 
join bn_bookingDtl as BDtl
on b.PKID=BDtl.BookingID 
join bn_BookingNoteDtl BookNDtl
on BDtl.BookingNoteDtlID =BookNDtl.PKID
join bn_BookingNote BookN 
on BookNDtl.BookingNoteID=Bookn.PKID
where Bookn.CreatedUser in(
select UserID from Users where  ClientID in(
select ClientID from Users where UserID=@UserID))

where 1=1 ";
        }
        else
        {
            sqlStr = @"SELECT st.PKID AS STID, stDtl.PKID AS STDtlID, soDtl.JobDate, 
shipper.ENGShortName AS Shipper, vendor.ENGShortName AS Vendor, 
 whse.ENGShortName AS Whse, soDtl.SONO, soDtl.VOL, cm.Model AS TruckType, contractor.ENGShortName AS Contractor, 
 truck.LicenseNO AS LinceseNO, Ts.StatusName, bnT.SendTruckID, bnT.CBDate
FROM         dbo.BN_SendTruck AS st INNER JOIN
                      dbo.BN_SendTruckDtl AS stDtl ON st.PKID = stDtl.SendTruckID INNER JOIN
                      dbo.BN_ShippingOrder AS so ON st.OrderID = so.PKID INNER JOIN
                      dbo.BN_ShippingOrderDtl AS soDtl ON so.PKID = soDtl.OrderID INNER JOIN
                      dbo.BN_TTracking AS bnT ON st.PKID = bnT.SendTruckID LEFT OUTER JOIN
                      dbo.Sy_TruckStatus AS Ts ON bnT.TruckStatusID = Ts.PKID INNER JOIN
                      dbo.Sy_Client AS shipper ON soDtl.ShipperID = shipper.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS vendor ON so.VendorID = vendor.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS whse ON soDtl.WhouseID = whse.PKID LEFT OUTER JOIN
                      dbo.Sy_ContainerModel AS cm ON stDtl.TypeID = cm.PKID LEFT OUTER JOIN
                      dbo.Sy_Client AS contractor ON so.Contractor = contractor.PKID LEFT OUTER JOIN
                      dbo.BN_Truck AS truck ON stDtl.TruckID = truck.PKID 
                    
where 1=1 ";
        }
        sqlStr += sqlWhere;
        return db.RunSqlTab(sqlStr, parms);
    }
    #endregion

    #region Preview Report
    private void PreviewReport()
    {
        rptViewer.LocalReport.ReportPath = "Reports/rptTruckStatus.rdlc";

        rptViewer.LocalReport.DataSources.Clear();
        DataTable dt = GetDataTable();
        if (dt != null && dt.Rows.Count > 0)
        {
            rptViewer.Visible = true;
            lblNoRecord.Visible = false;
            ReportDataSource datasource = new ReportDataSource("dsTruckStatus_TruckStatus", dt);
            rptViewer.LocalReport.DataSources.Add(datasource);
            rptViewer.LocalReport.Refresh();
        }
        else
        {
            rptViewer.Visible = false;
            lblNoRecord.Visible = true;
        }
    }
    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtJobDateFrom.Text != string.Empty)
        {
            ViewState["DateFrom"] = txtJobDateFrom.Text.Trim();
        }
        else
        {
            ViewState["DateFrom"] = null;
        }
        if (txtJobDateTo.Text != string.Empty)
        {
            ViewState["DateTo"] = txtJobDateTo.Text.Trim();
        }
        else
        {
            ViewState["Dateto"] = null;
        }
        if (lstSelShipper.Items.Count > 0)
        {
            string item = string.Empty;
            for (int i = 0; i < lstSelShipper.Items.Count; i++)
            {
                item += lstSelShipper.Items[i].Value + ",";
            }
            ViewState["Shipper"] = item.Substring(0, item.Length - 1);
        }
        else
        {
            ViewState["Shipper"] = null;
        }

        if (lstSelVendor.Items.Count > 0)
        {
            string item = string.Empty;
            for (int i = 0; i < lstSelVendor.Items.Count; i++)
            {
                item += lstSelVendor.Items[i].Value + ",";
            }
            ViewState["Vendor"] = item.Substring(0, item.Length - 1);
        }
        else
        {
            ViewState["Vendor"] = null;
        }

        if (lstSelWhse.Items.Count > 0)
        {
            string item = string.Empty;
            for (int i = 0; i < lstSelWhse.Items.Count; i++)
            {
                item += lstSelWhse.Items[i].Value + ",";
            }
            ViewState["Whse"] = item.Substring(0, item.Length - 1);
        }
        else
        {
            ViewState["Whse"] = null;
        }

        if (txtLinceseNo.Text != string.Empty)
        {
            ViewState["LinceseNo"] = txtLinceseNo.Text;
        }
        else
        {
            ViewState["LinceseNo"] = null;
        }

        PreviewReport();
    }

    protected void btnEmail_Click(object sender, EventArgs e)
    {
        if (this.rptViewer.LocalReport.DataSources.Count > 0)
        {
            Microsoft.Reporting.WebForms.Warning[] Warnings;
            string[] strStreamIds;
            string strMimeType;
            string strEncoding;
            string strFileNameExtension;

            byte[] bytes = this.rptViewer.LocalReport.Render("Excel", null, out strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);

            // string strFilePath = @"D:\report.xls";
            string filename = FileTools.AutoFileName() + ".xls";
            string strFilePath = "TruckStatusReport_" + filename;
            using (System.IO.FileStream fs = new FileStream(Server.MapPath("~").ToString() + "/EmailReport/" + strFilePath, FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
            }

            Response.Redirect("~/SystemManage/EmailClient.aspx?atm=" + strFilePath);
        }
        else
        {

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (lstShipper.SelectedValue != string.Empty)
        {
            for (int i = 0; i < lstShipper.Items.Count; i++)
            {
                bool issel = false;
                if (lstShipper.Items[i].Selected)
                {
                    for (int j = 0; j < lstSelShipper.Items.Count; j++)
                    {
                        if (lstShipper.Items[i].Value == lstSelShipper.Items[j].Value)
                        {
                            issel = true;
                            break;
                        }
                    }

                    if (!issel)
                    {
                        ListItem item = new ListItem();
                        item.Text = lstShipper.Items[i].Text;
                        item.Value = lstShipper.Items[i].Value;
                        lstSelShipper.Items.Add(item);
                    }
                }
            }
        }
        else
        {

        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (lstSelShipper.Items.Count > 0)
        {
            ListItemCollection lstColl = new ListItemCollection();
            for (int i = 0; i < lstSelShipper.Items.Count; i++)
            {
                if (!lstSelShipper.Items[i].Selected)
                {
                    ListItem item = new ListItem(lstSelShipper.Items[i].Text, lstSelShipper.Items[i].Value);
                    lstColl.Add(item);
                }
            }
            if (lstColl != null && lstColl.Count > 0)
            {
                lstSelShipper.Items.Clear();
                for (int j = 0; j < lstColl.Count; j++)
                {
                    lstSelShipper.Items.Add(lstColl[j]);
                }
            }
            else
            {
                lstSelShipper.Items.Clear();
            }

        }
    }

    //protected void btnCustomerAdd_Click(object sender, EventArgs e)
    //{
    //    if (lstCustomer.SelectedValue != string.Empty)
    //    {
    //        for (int i = 0; i < lstCustomer.Items.Count; i++)
    //        {
    //            bool issel = false;
    //            if (lstCustomer.Items[i].Selected)
    //            {
    //                for (int j = 0; j < lstSelCustomer.Items.Count; j++)
    //                {
    //                    if (lstCustomer.Items[i].Value == lstSelCustomer.Items[j].Value)
    //                    {
    //                        issel = true;
    //                        break;
    //                    }
    //                }

    //                if (!issel)
    //                {
    //                    ListItem item = new ListItem();
    //                    item.Text = lstCustomer.Items[i].Text;
    //                    item.Value = lstCustomer.Items[i].Value;
    //                    lstSelCustomer.Items.Add(item);
    //                }
    //            }
    //        }
    //    }
    //    else
    //    {

    //    }
    //}

    //protected void btnCustomerDel_Click(object sender, EventArgs e)
    //{
    //    if (lstSelCustomer.Items.Count > 0)
    //    {
    //        ListItemCollection lstColl = new ListItemCollection();
    //        for (int i = 0; i < lstSelCustomer.Items.Count; i++)
    //        {
    //            if (!lstSelCustomer.Items[i].Selected)
    //            {
    //                ListItem item = new ListItem(lstSelCustomer.Items[i].Text, lstSelCustomer.Items[i].Value);
    //                lstColl.Add(item);
    //            }
    //        }
    //        if (lstColl != null && lstColl.Count > 0)
    //        {
    //            lstSelCustomer.Items.Clear();
    //            for (int j = 0; j < lstColl.Count; j++)
    //            {
    //                lstSelCustomer.Items.Add(lstColl[j]);
    //            }
    //        }
    //        else
    //        {
    //            lstSelCustomer.Items.Clear();
    //        }

    //    }
    //}

    protected void btnWhseAdd_Click(object sender, EventArgs e)
    {
        if (lstWhse.SelectedValue != string.Empty)
        {
            for (int i = 0; i < lstWhse.Items.Count; i++)
            {
                bool issel = false;
                if (lstWhse.Items[i].Selected)
                {
                    for (int j = 0; j < lstSelWhse.Items.Count; j++)
                    {
                        if (lstWhse.Items[i].Value == lstSelWhse.Items[j].Value)
                        {
                            issel = true;
                            break;
                        }
                    }

                    if (!issel)
                    {
                        ListItem item = new ListItem();
                        item.Text = lstWhse.Items[i].Text;
                        item.Value = lstWhse.Items[i].Value;
                        lstSelWhse.Items.Add(item);
                    }
                }
            }
        }
        else
        {

        }
    }

    protected void btnWhseDel_Click(object sender, EventArgs e)
    {
        if (lstSelWhse.Items.Count > 0)
        {
            ListItemCollection lstColl = new ListItemCollection();
            for (int i = 0; i < lstSelWhse.Items.Count; i++)
            {
                if (!lstSelWhse.Items[i].Selected)
                {
                    ListItem item = new ListItem(lstSelWhse.Items[i].Text, lstSelWhse.Items[i].Value);
                    lstColl.Add(item);
                }
            }
            if (lstColl != null && lstColl.Count > 0)
            {
                lstSelWhse.Items.Clear();
                for (int j = 0; j < lstColl.Count; j++)
                {
                    lstSelWhse.Items.Add(lstColl[j]);
                }
            }
            else
            {
                lstSelWhse.Items.Clear();
            }

        }
    }

    protected void btnVendorAdd_Click(object sender, EventArgs e)
    {
        if (lstVendor.SelectedValue != string.Empty)
        {
            for (int i = 0; i < lstVendor.Items.Count; i++)
            {
                bool issel = false;
                if (lstVendor.Items[i].Selected)
                {
                    for (int j = 0; j < lstSelVendor.Items.Count; j++)
                    {
                        if (lstVendor.Items[i].Value == lstSelVendor.Items[j].Value)
                        {
                            issel = true;
                            break;
                        }
                    }

                    if (!issel)
                    {
                        ListItem item = new ListItem();
                        item.Text = lstVendor.Items[i].Text;
                        item.Value = lstVendor.Items[i].Value;
                        lstSelVendor.Items.Add(item);
                    }
                }
            }
        }
        else
        {

        }
    }

    protected void btnVendorDel_Click(object sender, EventArgs e)
    {
        if (lstSelVendor.Items.Count > 0)
        {
            ListItemCollection lstColl = new ListItemCollection();
            for (int i = 0; i < lstSelVendor.Items.Count; i++)
            {
                if (!lstSelVendor.Items[i].Selected)
                {
                    ListItem item = new ListItem(lstSelVendor.Items[i].Text, lstSelVendor.Items[i].Value);
                    lstColl.Add(item);
                }
            }
            if (lstColl != null && lstColl.Count > 0)
            {
                lstSelVendor.Items.Clear();
                for (int j = 0; j < lstColl.Count; j++)
                {
                    lstSelVendor.Items.Add(lstColl[j]);
                }
            }
            else
            {
                lstSelVendor.Items.Clear();
            }

        }
    }
}
